Imports MIMS.WRS.Business
Imports MIMS.WRS.Entities
Imports System.IO
Imports MyFramework

Public Class CompletionDateSupplierReport
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents rptList As System.Web.UI.WebControls.Repeater

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Protected WithEvents header As BOReportHeader

    Protected Function GetOwnerText(ByVal data As Object) As String
        Dim officeID As Integer = Convert.ToInt32(data)
        Try
            Return OfficeService.GetOffice(officeID).OfficeName
        Catch ex As Exception
            Return "[error]"
        End Try
    End Function

    Protected Function GetPackTypeText(ByVal data As Object) As String
        If data Is DBNull.Value Then
            Return "&nbsp;"
        Else
            Return BulkOrder.GetBOPackTypeMeaning(Convert.toString(data))
        End If
    End Function

    Protected Function GetTypeText(ByVal data As Object) As String
        If data Is DBNull.Value Then
            Return "&nbsp;"
        Else
            Return Utilities.GetBOTypeMeaning(CType(data, EBOType))
        End If
    End Function

    Protected Function GetPictureHTML(ByVal data As Object) As String
        If data Is DBNull.Value Then
            Return "&nbsp;"
        Else
            Dim filePath As String = String.Format("{0}.jpg", Path.Combine(Config.ProductImagePath, Convert.ToString(data)))

            If File.Exists(filePath) Then
                Return String.Format("<img src='../ViewPicture.aspx?procode={0}&size={1}' border=0 alt='{0}.jpg'>", data, Config.ProStatReportImageSize)
            Else
                Return "&nbsp;"
            End If
        End If
    End Function

    Private Function GetSource() As IDataReader
        Dim boType As Int32 = -1
        If Not Request.QueryString("type") Is Nothing Then
            boType = RequestContext.BOType
        End If

        Dim target As Int32 = -1
        If Not Request.QueryString("target") Is Nothing Then
            target = RequestContext.Target
        End If

        Return BulkOrderService.GetBulkOrdersCompletionDateSupplierReport(RequestContext.ConPointID, RequestContext.SupplierCode, _
               RequestContext.Owner, boType, target, RequestContext.ReportType, _
               RequestContext.Quantity, RequestContext.CurrentUser.LOGIN_NAME)
    End Function
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            If RequestContext.DoReport Then
                Me.rptList.DataSource = GetSource()
                Me.rptList.DataBind()
            End If
        End If
    End Sub

    Private Sub header_ExportPDFClick() Handles header.ExportPDFClick
        Dim source As IDataReader = GetSource()
        Dim boXml As String = RenderService.GenerateBOXML(source, Config.ProductImagePath, RequestContext.Discount, RequestContext.ShowFOB)
        Dim xslFile As String = Server.MapPath("../foxsl/foCompletionDateSupplierReport.xsl")
        Dim foFile As String = Server.MapPath("../foTemp/" + String.Format("{0}{1}{2}{3}{4}{5}{6}.fo", DateTime.Now.Year, _
                DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, _
                DateTime.Now.Second, DateTime.Now.Millisecond))
        Dim pdfFile As String = String.Format("BOCompletionDateSupplierReport_{0}{1}{2}{3}{4}{5}{6}.pdf", DateTime.Now.Year, _
                DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, _
                DateTime.Now.Second, DateTime.Now.Millisecond)
        Try
            If RenderService.GenerateFO(boXml, xslFile, foFile) Then
                Dim buffer() As Byte = RenderService.RenderFOtoPDF(foFile)
                File.Delete(foFile)

                Response.Clear()
                Response.AddHeader("Content-Disposition", String.Format("attachment;filename={0}", HttpUtility.UrlEncode(pdfFile)))
                Response.AddHeader("Content-Type", "application/pdf")
                Response.BinaryWrite(buffer)
                Response.Flush()
                Response.End()

            End If
        Catch ex As Exception
            Logger.Debug("Failed to render CompletionDateSupplierReport PDF", ex)
        End Try
    End Sub

    Private Sub header_OKClick(ByVal conPointID As Integer, ByVal suppCode As String, ByVal owner As Integer, ByVal type As Integer, ByVal target As Integer, ByVal discount As Decimal, ByVal showFOB As Boolean, ByVal qty As Integer, ByVal notAvailInUSA As Boolean) Handles header.OKClick
        Response.Redirect(String.Format("CompletionDateSupplierReport.aspx?sup={0}&owner={1}&type={2}&target={3}&rt={4}&discount={5}&showfob={6}&cpid={7}&naUSA={8}&qty={9}&dorpt=1", _
                  suppCode, owner, type, target, RequestContext.ReportType, discount, Convert.ToInt32(showFOB), conPointID, Convert.ToInt32(notAvailInUSA), qty))
    End Sub

End Class
